package com.pctx.bibapermissionorder.controller;

import com.github.pagehelper.PageHelper;
import com.pctx.bibacommon.result.Result;
import com.pctx.bibapermissionorder.pageHelper.PageInfo;
import com.pctx.bibapermissionorder.pojo.PermissionOrder;
import com.pctx.bibapermissionorder.service.PermissionOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

/**
 * Created by chason on 2018/3/2.11:17
 */
@RestController
@RequestMapping("permissionOrder")
public class PermissionOrderControoler {
    @Autowired
    private PermissionOrderService permissionOrderService;

    @GetMapping("page")
    public Result page(@RequestParam(value = "companyUuid", required = false) String companyUuid,
                       @RequestParam(value = "beginTime", required = false) Date beginTime,
                       @RequestParam(value = "status", required = false) Byte status,
                       @RequestParam(value = "page", defaultValue = "1") Integer page,
                       @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize) throws Exception {
        PageHelper.startPage(page, pageSize);
        List<PermissionOrder> list = permissionOrderService.getByCompanyUuidOrUserUuid(companyUuid, status, beginTime);
        return Result.success(new PageInfo<PermissionOrder>(list));
    }

    @PostMapping("add")
    public Result add(@RequestParam(value = "companyUuid", required = true) String companyUuid,
                      @RequestParam(value = "permissionUuid", required = true) String permissionUuid,
                      @RequestParam(value = "permissionPackageUuid", required = true) String permissionPackageUuid,
                      @RequestParam(value = "amount", required = true) BigDecimal amount,
                      @RequestParam(value = "payType", required = true) Byte payType) throws Exception {
        PermissionOrder permissionOrder = permissionOrderService.addOrder(companyUuid, permissionUuid,
                permissionPackageUuid, amount, payType);
        if (null == permissionOrder)
            return Result.error("下单失败");
        else
            return Result.success(permissionOrder, "下单成功");
    }
}
